package ui;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.ConnectException;
import java.rmi.RemoteException;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import RMI.Client;
import Util.Controle;

import model.TableModel;
import model.TableModel.modelTipo;
import Entidades.Filial;
import Entidades.Projeto;
import Entidades.Usuario;

public class JanelaPrincipal extends JFrame {

	private JPanel contentPane;
	private JTextField txtCodigo;
	private JTextField txtProjeto;
	private JTextField txtCusto;
	private JTable table;
	private TableModel tableModel;
	private JComboBox comboUsuarios;
	private JLabel labelOnline;


	public JanelaPrincipal() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 798, 562);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JLabel lblNewLabel = new JLabel("FILIAL " + Controle.getFilialnumero());
		contentPane.add(lblNewLabel, BorderLayout.SOUTH);
		
		JToolBar toolBar = new JToolBar();
		toolBar.setFont(new Font("Segoe UI", Font.PLAIN, 14));
		contentPane.add(toolBar, BorderLayout.NORTH);
		
		labelOnline = new JLabel(Client.ListaOnline());
		toolBar.add(labelOnline);
		
		
		
/*		JLabel lblF2 = new JLabel("   F02   ");
		toolBar.add(lblF2);
		JLabel lblF3 = new JLabel("   F03   ");
		toolBar.add(lblF3);
		JLabel lblF4 = new JLabel("   F04   ");
		toolBar.add(lblF4);
		JLabel lblF5 = new JLabel("   F05   ");
		toolBar.add(lblF5);
		JLabel lblF6 = new JLabel("   F06   ");
		toolBar.add(lblF6);
		JLabel lblF7 = new JLabel("   F07   ");
		toolBar.add(lblF7);
		JLabel lblF8 = new JLabel("   F08   ");
		toolBar.add(lblF8);
		JLabel lblF9 = new JLabel("   F09   ");
		toolBar.add(lblF9);
		JLabel lblF10 = new JLabel("F10");
		lblF10.setHorizontalAlignment(SwingConstants.CENTER);
		toolBar.add(lblF10);*/
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBorder(new EmptyBorder(15, 0, 0, 0));
		contentPane.add(tabbedPane, BorderLayout.CENTER);
		
		JPanel panelProjetos = new JPanel();
		panelProjetos.setBorder(new EmptyBorder(10, 0, 0, 0));
		tabbedPane.addTab("Projetos", null, panelProjetos, null);
		GridBagLayout gbl_panelProjetos = new GridBagLayout();
		gbl_panelProjetos.columnWidths = new int[]{117, 248, 0, 0};
		gbl_panelProjetos.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0};
		gbl_panelProjetos.columnWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
		gbl_panelProjetos.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		panelProjetos.setLayout(gbl_panelProjetos);
		
		JLabel lblNewLabel_1 = new JLabel("C\u00F3digo");
		GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
		gbc_lblNewLabel_1.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_1.gridx = 0;
		gbc_lblNewLabel_1.gridy = 0;
		panelProjetos.add(lblNewLabel_1, gbc_lblNewLabel_1);
		
		txtCodigo = new JTextField();
		GridBagConstraints gbc_txtCodigo = new GridBagConstraints();
		gbc_txtCodigo.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtCodigo.insets = new Insets(0, 0, 5, 5);
		gbc_txtCodigo.gridx = 1;
		gbc_txtCodigo.gridy = 0;
		panelProjetos.add(txtCodigo, gbc_txtCodigo);
		txtCodigo.setColumns(10);
		
		JLabel lblNome = new JLabel("Projeto");
		GridBagConstraints gbc_lblNome = new GridBagConstraints();
		gbc_lblNome.anchor = GridBagConstraints.EAST;
		gbc_lblNome.insets = new Insets(0, 0, 5, 5);
		gbc_lblNome.gridx = 0;
		gbc_lblNome.gridy = 1;
		panelProjetos.add(lblNome, gbc_lblNome);
		
		txtProjeto = new JTextField();
		txtProjeto.setColumns(10);
		GridBagConstraints gbc_txtProjeto = new GridBagConstraints();
		gbc_txtProjeto.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtProjeto.insets = new Insets(0, 0, 5, 5);
		gbc_txtProjeto.gridx = 1;
		gbc_txtProjeto.gridy = 1;
		panelProjetos.add(txtProjeto, gbc_txtProjeto);
		
		JLabel lblNewLabel_2 = new JLabel("Gerente");
		GridBagConstraints gbc_lblNewLabel_2 = new GridBagConstraints();
		gbc_lblNewLabel_2.anchor = GridBagConstraints.NORTHEAST;
		gbc_lblNewLabel_2.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_2.gridx = 0;
		gbc_lblNewLabel_2.gridy = 2;
		panelProjetos.add(lblNewLabel_2, gbc_lblNewLabel_2);
		
		comboUsuarios = new JComboBox();
		GridBagConstraints gbc_comboUsuarios = new GridBagConstraints();
		gbc_comboUsuarios.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboUsuarios.insets = new Insets(0, 0, 5, 5);
		gbc_comboUsuarios.gridx = 1;
		gbc_comboUsuarios.gridy = 2;
		panelProjetos.add(comboUsuarios, gbc_comboUsuarios);
		
		JLabel lblCustoTotal = new JLabel("Custo total");
		GridBagConstraints gbc_lblCustoTotal = new GridBagConstraints();
		gbc_lblCustoTotal.insets = new Insets(0, 0, 5, 5);
		gbc_lblCustoTotal.anchor = GridBagConstraints.EAST;
		gbc_lblCustoTotal.gridx = 0;
		gbc_lblCustoTotal.gridy = 3;
		panelProjetos.add(lblCustoTotal, gbc_lblCustoTotal);
		
		txtCusto = new JTextField();
		txtCusto.setColumns(10);
		GridBagConstraints gbc_txtCusto = new GridBagConstraints();
		gbc_txtCusto.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtCusto.insets = new Insets(0, 0, 5, 5);
		gbc_txtCusto.gridx = 1;
		gbc_txtCusto.gridy = 3;
		panelProjetos.add(txtCusto, gbc_txtCusto);
		
		JButton btnAdicionar = new JButton("Adicionar");
		btnAdicionar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				adicionaProjeto();
			}
		});
		GridBagConstraints gbc_btnAdicionar = new GridBagConstraints();
		gbc_btnAdicionar.anchor = GridBagConstraints.EAST;
		gbc_btnAdicionar.insets = new Insets(0, 0, 5, 5);
		gbc_btnAdicionar.gridx = 1;
		gbc_btnAdicionar.gridy = 4;
		panelProjetos.add(btnAdicionar, gbc_btnAdicionar);
		
		
		tableModel = new TableModel(modelTipo.PROJETO);
		
		JScrollPane scrollPane = new JScrollPane();
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.insets = new Insets(0, 0, 0, 5);
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridwidth = 3;
		gbc_scrollPane.gridx = 0;
		gbc_scrollPane.gridy = 5;
		panelProjetos.add(scrollPane, gbc_scrollPane);
		
		table = new JTable();
		scrollPane.setViewportView(table);
		table.setModel(tableModel);
		
		
		JPanel panelTarefas = new JPanel();
		tabbedPane.addTab("Tarefas", null, panelTarefas, null);
		panelTarefas.setLayout(null);
		
		JLabel lblProjeto = new JLabel("Projeto");
		lblProjeto.setBounds(10, 14, 51, 14);
		panelTarefas.add(lblProjeto);
		
		JComboBox comboBox_1 = new JComboBox();
		comboBox_1.setBounds(57, 11, 133, 20);
		panelTarefas.add(comboBox_1);
		
		JPanel panelUsuarios = new JPanel();
		tabbedPane.addTab("Usu\u00E1rios", null, panelUsuarios, null);
		panelUsuarios.setLayout(null);
		
		JLabel label = new JLabel("Filial");
		label.setBounds(10, 14, 41, 14);
		panelUsuarios.add(label);
		
		JComboBox comboBox_2 = new JComboBox();
		comboBox_2.setBounds(61, 11, 133, 20);
		panelUsuarios.add(comboBox_2);
		
		inicializa();
		
		Timer timer = new Timer();
		
		timer.schedule(new TimerTask() {
			
			@Override
			public void run() {
				labelOnline.setText(Client.ListaOnline());
			}
		},1000,1000);			
		
	}
	
	private void inicializa(){
		
		tableModel.adicionaProjetos(Filial.getInstance().getProjetos());
		
		for (Usuario usuario : Filial.getInstance().getUsuarios()) {
			comboUsuarios.addItem(usuario);
		}
	}
	
	private void adicionaProjeto(){
		Projeto p  = new Projeto(txtCodigo.getText().trim(), txtProjeto.getText());
		//p.setGerente(comboUsuarios.getSelectedIndex());
		p.setCustoTotal(Double.parseDouble(txtCusto.getText()));
		p.setStatus(Integer.parseInt(txtCusto.getText()));
		Filial.getInstance().adicionaProjeto(p);
		tableModel.adicionaProjeto(p);
	}
	
}
